﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using pygs.Security;
using pygs.Messaging;
using pygs.UI;
using pygs.Locator;
using pygs.Classes;

namespace pygs.Application
{
    public class MainApplication : CustomApplication
    {
        private CustomSecurity _security;
        public CustomSecurity security { get { return _security; } }

        private CustomMessaging _messaging;
        public CustomMessaging messaging { get { return _messaging; } }

        private CustomUI _ui;
        public CustomUI ui { get { return _ui; } }

        private CustomLocator _locator;
        public CustomLocator locator { get { return _locator; } }

        protected string securityToken { get; set; }
        
        protected override void InitilizeComponent()
        {
            base.InitilizeComponent();
            _security = (CustomSecurity)context.GetImplementation("security");
            _messaging = (CustomMessaging)context.GetImplementation("messaging");
            _ui = (CustomUI)context.GetImplementation("ui");
            _locator = (CustomLocator)context.GetImplementation("locator");
            mainThread = new Thread(DoMainThread);
        }

        protected override void DoStartUp()
        {
            base.DoStartUp();
            ui.Command += DoUICommand;
            locator.NewLocation += DoNewLocation;
        }

        protected override void DoShutDown()
        {
            locator.NewLocation -= DoNewLocation;
            ui.Command -= DoUICommand;
            base.DoShutDown();
        }

        protected virtual void DoUICommand(CustomUI sender, UICommand command)
        {
        }

        protected virtual void DoNewLocation(CustomLocator locator, Location location)
        {
            messaging.Send(new LocationMessage(securityToken, location));
            ui.PerformStatus(new UIStatusLocation(location));
        }

        private Thread mainThread;
        protected Thread MainThread { get { return mainThread; } }

        protected bool isRunned { get; set; }

        protected void ProccessLocations()
        {
            locator.ProccessNewLocations();
        }

        protected void ProccessCommands()
        {
        }

        protected virtual void DoMainThread()
        {
            while (isRunned)
            {
                ProccessLocations();
                ProccessCommands();
                Thread.Sleep(1000);
            }
        }

        protected virtual void DoRun()
        {
            string securityToken = "";
            if (!security.Register(out securityToken))
                throw new Exception("security error");
            this.securityToken = securityToken;
            isRunned = true;
            locator.Start();
            MainThread.Start();
        }

        protected virtual void DoStop()
        {
            isRunned = false;
            locator.Stop();
            security.Unregister(securityToken);
        }

        public override void Run()
        {
            DoRun();
            ui.RunUI();
            DoStop();
        }
    }
}
